package com.ruoyi.web.controller.api;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.MallBanner;
import com.ruoyi.mall.domain.MallCmsNotice;
import com.ruoyi.mall.domain.MallImg;
import com.ruoyi.mall.domain.vo.IndexBannerVO;
import com.ruoyi.mall.domain.vo.IndexGoodsVO;
import com.ruoyi.mall.domain.vo.IndexNoticeVO;
import com.ruoyi.mall.service.IMallBannerService;
import com.ruoyi.mall.service.IMallCmsNoticeService;
import com.ruoyi.mall.service.IMallGoodsGoodsService;
import com.ruoyi.mall.service.IMallImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApiIndexController
 * @Description 请描述类的业务用途
 * @Author linlinee
 * @Date 2021/11/29 4:25 PM
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api/index")
@ResponseBody
public class ApiIndexController {

    @Autowired
    IMallGoodsGoodsService iMallGoodsGoodsService;

    @Autowired
    IMallBannerService iMallBannerService;

    @Autowired
    IMallCmsNoticeService iMallCmsNoticeService;
    @Autowired
    IMallImgService imgService;

    @GetMapping("/getIndexData")
    public ApiResult getIndexData(){
        List<IndexGoodsVO> saleList = iMallGoodsGoodsService.selectMallGoodsBySales();
        for (IndexGoodsVO indexGoodsVO: saleList) {
            MallImg mallImg = new MallImg();
            mallImg.setResId(indexGoodsVO.getId());
            mallImg.setType("0");
            List<String> list = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setBanners(list);

            mallImg.setType("1");
            List<String> detailList = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setDetailUrl(detailList);
        }
        List<IndexGoodsVO> timeList = iMallGoodsGoodsService.selectMallGoodsByCreatTime();
        for (IndexGoodsVO indexGoodsVO: timeList) {
            MallImg mallImg = new MallImg();
            mallImg.setResId(indexGoodsVO.getId());
            mallImg.setType("0");
            List<String> list = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setBanners(list);

            mallImg.setType("1");
            List<String> detailList = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setDetailUrl(detailList);
        }
        List<IndexGoodsVO> hotList = iMallGoodsGoodsService.selectMallGoodsByhot();
        for (IndexGoodsVO indexGoodsVO: hotList) {
            MallImg mallImg = new MallImg();
            mallImg.setResId(indexGoodsVO.getId());
            mallImg.setType("0");
            List<String> list = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setBanners(list);

            mallImg.setType("1");
            List<String> detailList = imgService.selectMallImgUrlVoList(mallImg);
            indexGoodsVO.setDetailUrl(detailList);
        }
        List<IndexBannerVO> banners = iMallBannerService.indexSelectMallBannerList(new MallBanner());
        List<IndexNoticeVO> notices = iMallCmsNoticeService.indexSelectMallCmsNoticeList(new MallCmsNotice());
        Map<String, Object> map = new HashMap<>();
        map.put("saleList",saleList);
        map.put("timeList",timeList);
        map.put("hotList",hotList);
        map.put("banners",banners);
        map.put("notices",notices);
        return ApiResult.ok().put("data",map);
    }

    @GetMapping("/getClassIndexBanner")
    public ApiResult getClassIndexBanner(){
        List<IndexBannerVO> banners = iMallBannerService.classIndexSelectMallBannerList(new MallBanner());
        return ApiResult.ok().put("data",banners);
    }
}
